package com.jasperframework.boot.common.modules.redis.publish;

import com.jasperframework.boot.common.constant.JasperConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 消息的发布者
 */
@Configuration
@Slf4j
public class DefaultRedisPublisher {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public void publish(Map<String, Object> params) {
        if(params.isEmpty()){
            return ;
        }
        if(!params.containsKey(JasperConstants.JASPER_HANDLER_NAME)) {
            params.put(JasperConstants.JASPER_HANDLER_NAME, JasperConstants.JASPER_DEFAULT_REDIS_SUBSCRIBER);
        };
        params.put(JasperConstants.JASPER_DEFAULT_REDIS_TOPIC_KEY, JasperConstants.JASPER_DEFAULT_REDIS_TOPIC);
        // 发送消息到 Redis 主题
        this.redisTemplate.convertAndSend(JasperConstants.JASPER_DEFAULT_REDIS_TOPIC, params);
    }
}
